<?php

namespace App\Modules\Admin\Entities\Log;

use App\Models\Model;

class WaterDeviceOnlineLogModel extends Model
{

    protected $table='water_device_online_log';
    protected $primaryKey='id';

    protected function addLog($param)
    {

        $create =[
            'month'=>date('Y-m-d'),
            'water_id'=>$param['water_id'],
            'water_title'=>$param['water_title']
        ];
        $this->create($create);
    }
    public function createOnlineLog($water_id,$water_title,$type)
    {
        if (!$this->where(['water_id'=>$water_id,'month'=>date('Y-m-d')])->first()){
            $this->addLog([
                'water_id'=>$water_id,
                'water_title'=>$water_title
            ]);
        }

        $this->sumIncrement($water_id);
        switch ($type){
            case 'success':
                $this->successIncrement($water_id);
                break;
            case "error_failed":
                $this->errorFailedIncrement($water_id);
                break;
            case "error_busy":
                $this->errorBusyIncrement($water_id);
                break;
        }
        return true;
    }
    public function sumIncrement($id)
    {
        return $this->where(['water_id'=>$id,'month'=>date('Y-m-d')])->increment('sum',1);
    }
    public function successIncrement($id)
    {
        return $this->where(['water_id'=>$id,'month'=>date('Y-m-d')])->increment('success',1);
    }
    public function errorFailedIncrement($id)
    {
        return $this->where(['water_id'=>$id,'month'=>date('Y-m-d')])->increment('error_failed',1);
    }
    public function errorBusyIncrement($id)
    {
        return $this->where(['water_id'=>$id,'month'=>date('Y-m-d')])->increment('error_busy',1);
    }
}
